package com.huike.game.Activity;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.huike.game.R;
import com.huike.game.api.Api;
import com.huike.game.base.Game60;
import com.huike.game.view.SideView;

import org.json.JSONObject;

import java.util.ArrayList;

import androidx.appcompat.app.AppCompatActivity;

public class GameActivity60 extends AppCompatActivity {

    private ImageView iv_left_image;
    private TextView tv_left_text;
    private TextView tv_content_title;
    private ImageView iv_rigth_image;
    private TextView tv_right_text;
    private ListView lv_game60;
    private RequestQueue queue;
    private ProgressDialog show;
    private Game60 json;
    private ArrayList<Game60.DataBean> list;
    private ViewList viewList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game60);
        queue = Volley.newRequestQueue(this);
        list = new ArrayList<>();
        initView();
        initData();

    }

    private void show() {
        show = ProgressDialog.show(this, "提示", "加载中...");
        show.show();
    }

    private void hide() {
        show.dismiss();
    }

    private void initData() {
        show();
        JsonObjectRequest request = new JsonObjectRequest(1, Api.api + "UserInfo", new Response.Listener<JSONObject>() {



            @Override
            public void onResponse(JSONObject jsonObject) {
                hide();
                json = new Gson().fromJson(jsonObject.toString(), Game60.class);

                for (int i = 0; i<json.getData().size();i++){
                    list.add(json.getData().get(i));
                }

                viewList = new ViewList();
                lv_game60.setAdapter(viewList);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                hide();
                Log.e("---",volleyError.getMessage() + "0000");
                Toast.makeText(GameActivity60.this, "网络加载失败！", Toast.LENGTH_SHORT).show();
            }
        });

        queue.add(request);

    }

    private void initView() {
        iv_left_image = (ImageView) findViewById(R.id.iv_left_image);
        tv_left_text = (TextView) findViewById(R.id.tv_left_text);
        tv_content_title = (TextView) findViewById(R.id.tv_content_title);
        iv_rigth_image = (ImageView) findViewById(R.id.iv_rigth_image);
        tv_right_text = (TextView) findViewById(R.id.tv_right_text);
        lv_game60 = (ListView) findViewById(R.id.lv_game60);

        iv_rigth_image.setVisibility(View.GONE);
        iv_left_image.setImageResource(R.drawable.tiao);
        tv_content_title.setText("用户管理");
        tv_right_text.setText("收藏");
        tv_left_text.setVisibility(View.GONE);

        //点击打卡收藏页面
        tv_right_text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            startActivity(new Intent(GameActivity60.this,Game60CollectActivity.class));

            }
        });

    }

    class ViewList extends BaseAdapter {

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int i) {
            return list.get(i);
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(final int i, View view, ViewGroup viewGroup) {
            ViewHolder holder = null;
            if (view == null) {
                view = View.inflate(GameActivity60.this, R.layout.lv_game60, null);
                holder = new ViewHolder(view);
                view.setTag(holder);

            } else {
                holder = (ViewHolder) view.getTag();
            }

            Game60.DataBean bean = list.get(i);

            holder.tv_game60_username.setText("用户名：" + bean.getUser());
            holder.tv_game60_name.setText("姓名：" + bean.getName());
            holder.tv_game60_phone.setText("电话：" + bean.getPhone());

            final SideView sideView = (SideView) view;

            //收藏按钮点击
            holder.tv_game60_collect.setOnClickListener(new View.OnClickListener() {
                @SuppressLint("ApplySharedPref")
                @Override
                public void onClick(View view) {

                    //获取缓存数据
                    SharedPreferences sp = getSharedPreferences("collectList", MODE_PRIVATE);
                    String data = sp.getString("data", null);
                    //判断有没有添加过


                    if(data != null){
                        String[] split = data.split("-");
                        ArrayList<Game60.DataBean> dataBeans = new ArrayList<>();
                        for (int j=0;j<split.length;j++){
                            Game60.DataBean fromJson = new Gson().fromJson(split[j], Game60.DataBean.class);
                            dataBeans.add(fromJson);

//                        判断是否添加
                            if(list.get(i).getUser().equals(dataBeans.get(j).getUser())){
                                Toast.makeText(GameActivity60.this, "已经收藏过了无需在收藏了", Toast.LENGTH_SHORT).show();
                                sideView.close();
                                return;
                            }

                        }
                    }

                    //将对象转换为字符串
                    String json = new Gson().toJson(list.get(i));

                    //判断是不是为空
                    StringBuffer buffer;
                    if(data == null){
                        buffer = new StringBuffer();
                        buffer.append(json).append("-");
                    }else{
                        buffer = new StringBuffer(data);
                        buffer.append(json).append("-");
                    }

                    boolean commit = sp.edit().putString("data", buffer.toString()).commit();

                    if(commit){
                        Toast.makeText(GameActivity60.this, "收藏成功", Toast.LENGTH_SHORT).show();
                        sideView.close();
                    }else{
                        Toast.makeText(GameActivity60.this, "收藏失败！", Toast.LENGTH_SHORT).show();
                        sideView.close();
                    }


                }
            });


            //删除按钮点击
            holder.tv_Game60_cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    list.remove(i);
                    viewList.notifyDataSetChanged();

                    sideView.close();
                }
            });


            return view;
        }

        class ViewHolder {
            public View rootView;
            public ImageView iv_game60_head;
            public TextView tv_game60_username;
            public TextView tv_game60_name;
            public TextView tv_game60_phone;
            public TextView tv_game60_collect;
            public TextView tv_Game60_cancel;

            public ViewHolder(View rootView) {
                this.rootView = rootView;
                this.iv_game60_head = (ImageView) rootView.findViewById(R.id.iv_game60_head);
                this.tv_game60_username = (TextView) rootView.findViewById(R.id.tv_game60_username);
                this.tv_game60_name = (TextView) rootView.findViewById(R.id.tv_game60_name);
                this.tv_game60_phone = (TextView) rootView.findViewById(R.id.tv_game60_phone);
                this.tv_game60_collect = (TextView) rootView.findViewById(R.id.tv_game60_collect);
                this.tv_Game60_cancel = (TextView) rootView.findViewById(R.id.tv_Game60_cancel);
            }

        }
    }

}
